package com.spsy.order.controller;
import com.alibaba.fastjson.JSON;
import com.spsy.common.core.domain.R;
import com.spsy.common.core.utils.DateUtils;
import com.spsy.common.core.utils.uuid.UUID;
import com.spsy.order.service.OrderOpsService;
import com.spsy.pay.api.SpsyPayProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Set;

/**
 * @author: liuzhiping
 * @date: 2024/12/17 11:33
 **/
@RestController
@Slf4j
public class OrderAppController {


    @Autowired
    private SpsyPayProvider spsyPayProvider;

    @Autowired
    private OrderOpsService orderOpsService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @GetMapping("/order/orderPay")
    public R orderPay(@RequestParam(name = "userId") String userId,@RequestParam(name = "productId") String productId) throws Exception
    {

        String orderId = UUID.randomUUID().toString();
        log.info("TestController 下单开始 orderId:{}",orderId);
        return R.ok(orderOpsService.pay(orderId,userId,productId));


    }


    @GetMapping("/order/redis")
    public R testRedis() throws Exception
    {

        //分数、杀怪数量 数字类型  ；时间类型>long  1970  wangdonghui 2024-1【100001】  baiyang 20224-12【100009】
        String key = "scoreRankings";
        /*this.stringRedisTemplate.opsForZSet().add(key,"wangdonghui",order.getFee());
        this.stringRedisTemplate.opsForZSet().add(key,"baiyang",100009);*/
        /***
         * 此处还有30位同学,.......
         */

        this.stringRedisTemplate.opsForZSet().add(key,"wangyongfa",98);

        this.stringRedisTemplate.opsForZSet().add(key,"tiajiakuan",60);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",67);

        this.stringRedisTemplate.opsForZSet().add(key,"wangwu",34);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",67);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",87);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu",23);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",66);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",77);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu",89);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",23);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",45);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu1",99);

        this.stringRedisTemplate.opsForZSet().add(key,"zhansan",87);
        this.stringRedisTemplate.opsForZSet().add(key,"lisi",43);
        this.stringRedisTemplate.opsForZSet().add(key,"wangwu",56);

       // Set<String> set = this.stringRedisTemplate.opsForZSet().r

        Set<String> set = this.stringRedisTemplate.opsForZSet().range(key,0,-1);

        Set<String> set2 = this.stringRedisTemplate.opsForZSet().rangeByScore(key,89,98);

        long count = this.stringRedisTemplate.opsForZSet().zCard(key);

        Set set3 = this.stringRedisTemplate.opsForZSet().popMax(key,3);

        long count1 = this.stringRedisTemplate.opsForZSet().zCard(key);
        log.info("count1:{} count:{} ",count1,count);









        log.info("set:"+ JSON.toJSONString(set));
        log.info("set2:"+ JSON.toJSONString(set2));
        log.info("set3:"+ JSON.toJSONString(set3));







        return  R.ok();
    }



    @GetMapping("/order/redis2")
    public R testRedis2() throws Exception
    {


        Date date = new Date();
        Date date1 = DateUtils.dateTime("yyyyMMdd","20241201");

        Date date2 = DateUtils.dateTime("yyyyMMdd","202412");

        Date date3 = DateUtils.dateTime("yyyyMMdd","20241202");

        //数据库的数据导入进去
        String key = "orderRankings";
        this.stringRedisTemplate.opsForZSet().add(key,JSON.toJSONString(OrderTest.builder().createTime(date1).id(1).build()),date1.getTime());
        this.stringRedisTemplate.opsForZSet().add(key,JSON.toJSONString(OrderTest.builder().createTime(date2).id(2).build()),date2.getTime());

        Set set10 = this.stringRedisTemplate.opsForZSet().rangeByScore(key,date1.getTime(),date2.getTime());

        log.info("set10:"+ JSON.toJSONString(set10));





        return  R.ok();
    }

   /* @GetMapping("/order/redis")
    public R testRedis() throws Exception
    {

        this.stringRedisTemplate.opsForValue().set("test:orderid:1","a12345678");

        return  R.ok();
    }*/




}
